package data.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import connexionBd.ConnexionBd;
import data.bean.Maison;

public class MaisonDAO {

	private final static Connection connexion = ConnexionBd.getConnection();

	public static Maison select(int idMaison) {
		Maison maison = null;
		try {
			Statement statement = connexion.createStatement();
			String sql = "SELECT * FROM energie.maison WHERE id_Maison = " + idMaison;
//			System.out.println(sql);
			ResultSet result = statement.executeQuery(sql);
			if (result.first()) {
				maison = new Maison(idMaison);
			}
			result.close();
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return maison;
	}

	public static boolean insert(Maison maison) {
		boolean result = false;
		try {
			Statement statement = connexion.createStatement();
			String sql = "INSERT INTO energie.maison (id_Maison) "
					+ "VALUES (" + maison.getId() + ")";
//			System.out.println(sql);
			int statut = statement.executeUpdate(sql);
			result = (statut == 1);
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	public static String getRequeteInsert(Maison maison) {
			String sql = "INSERT INTO energie.maison (id_Maison) "
					+ "VALUES (" + maison.getId() + ")";
		return sql;
	}

	public static boolean delete(int idMaison) {
		boolean result = false;
		try {
			Statement statement = connexion.createStatement();
			String sql = "DELETE FROM energie.maison WHERE id_Maison = " + idMaison;
			System.out.println(sql);
			int statut = statement.executeUpdate(sql);
			result = (statut == 1);
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
}
